# -*- coding: utf-8 -*-


# cmap2owl -- Helper application to convert from concept maps to OWL ontologies
# Copyright (c) 2008-2013  Rodrigo Rizzi Starr
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


'''
Finds if two relations are inverse one from the other

@author: Rodrigo Rizzi Starr
@copyright: Copyright © 2008-2013 Rodrigo Rizzi Starr
@license: MIT License
@contact: rodrigo.starr@gmail.com
'''


from mapperRule import MapperRule
from mapperQuestion import MapperQuestion
from infoProvider import InfoProvider
from log import log
log.debug("Loaded...")


class InverseRelationRule(MapperRule):
    "Simple rule to identify an inverse relation"
    
    def __init__(self, db):
          
        rules = [
                 "inferenceGroup(inverseRelationRule, [inverseRelation(_, _)])",
                 
                 "inverseRelation(R1, R2) :- "
                    "relationBetween(R1, F1, T1), "
                    "relationBetween(R2, T1, F1), "
                    "positive(relation(R1)), "
                    "positive(relation(R2)), "
                    "R1 \== R2, "
                    "forall(relationBetween(R1, From, To), "
                           "relationBetween(R2, To, From))",
#                    "forall(toPartRelation(R1, T), fromPartRelation(R2, T))",
        # relationBetween(LinkingPhrase, From, To)

                 "inverseOfOWL(R1, R2) :- "
                    "isTrue(inverseRelation(R1, R2))",
                 "inverseOfOWL(R2, R1) :- "
                    "isTrue(inverseRelation(R1, R2))",
                 "isTrue(relation(Relation)) :- "
                    "isTrue(inverseRelation(Relation, _))",
                 "isTrue(relation(Relation)) :- "
                    "isTrue(inverseRelation(_, Relation))",
                 ]
          
        MapperRule.__init__(self, "InverseRelations", db, rules)

    def getQuestions(self):
        "Returns a list of questions"
        questions = []
        query = "possible(inverseRelation(R1, R2))"
        
        relations = self.db.query(query)
        pairs = []
        for answer in relations:
            # Check to see if this pair has already been asked
            pair = (answer["R2"], answer["R1"])
            if pair not in pairs:
                pairs.append((pair[1], pair[0]))
                answer["Ids"] = set([answer["R1"], answer["R2"]])
                questions.append(MapperQuestion(
                    self.db,
                    answer,
                    'Are the relations "<b>%(R1)s</b>" and "<b>%(R2)s</b>" '
                    'inverse of each other ?',
                    "inverseRelation(%(R1)s, %(R2)s)",
                     helpFile = "inverse",
                  ))
              
        return questions

    def getInfoProvider(self):
        "Returns an info provider about mereologic relations"
        
        return InfoProvider(self.db,
                            "Rel =%s,"
                                "(isTrue(inverseRelation(Inverse, Rel));"
                                   "isTrue(inverseRelation(Rel, Inverse))),"
                                   "Set = [Inverse]",
                            "This relation is the inverse of:")
        
       
